﻿#include "prosetpage.h"
#include "ui_prosetpage.h"
#include <QDir>
#include <QFileDialog>
#include <QDebug>

ProSetPage::ProSetPage(QWidget *parent) :
    QWizardPage(parent),
    ui(new Ui::ProSetPage)
{
    ui->setupUi(this);
    registerField("proPath",ui->lineEditPath);
    registerField("proName*",ui->lineEditName);
    connect(ui->lineEditName,&QLineEdit::textEdited,this,&ProSetPage::completeChanged);
    connect(ui->lineEditPath,&QLineEdit::textEdited,this,&ProSetPage::completeChanged);
    auto path = QDir::currentPath();
    ui->lineEditPath->setText(path);
    ui->lineEditPath->setCursorPosition(ui->lineEditPath->text().size());
    ui->lineEditName->setClearButtonEnabled(true);
    ui->lineEditPath->setClearButtonEnabled(true);
}

ProSetPage::~ProSetPage()
{
    delete ui;
}


bool ProSetPage::isComplete() const
{
    if(ui->lineEditName->text() == "" || ui->lineEditPath->text() == ""){
        return false;
    }

    //判断是否文件夹是否合理
    QDir dir(ui->lineEditPath->text());
    if(!dir.exists())
    {
      // qDebug()<<"file path is not exists" << endl;
       ui->tips->setText("project path is not exists");
       return false;
    }

    //判断路径是否存在
    QString absFilePath = dir.absoluteFilePath(ui->lineEditName->text());
  //  qDebug() << "absFilePath is " << absFilePath;

    QDir dist_dir(absFilePath);
    if(dist_dir.exists()){
        ui->tips->setText("project has exists, change path or name!");
        return false;
    }

    ui->tips->setText("");
    return QWizardPage::isComplete();
}
void ProSetPage::GetProSettings(QString &name, QString &path)
{
    name = ui->lineEditName->text();
    path = ui->lineEditPath->text();
}

void ProSetPage::on_browse_clicked()
{
    QFileDialog file_dialog;
    file_dialog.setFileMode(QFileDialog::Directory);
    file_dialog.setWindowTitle("选择导入的文件夹");
    auto path = QDir::currentPath();
    file_dialog.setDirectory(path);
    file_dialog.setViewMode(QFileDialog::Detail);

    QStringList fileNames;
    if (file_dialog.exec()){
        fileNames = file_dialog.selectedFiles();
    }

    if(fileNames.length() <= 0){
         return;
    }

    QString import_path = fileNames.at(0);
 //   qDebug() << "import_path is " << import_path << endl;
    ui->lineEditPath->setText(import_path);
}
